

var googleMapScripts_loadTasks=false;		// true to load all tasks on page load
var googleMapScripts_loadTeams=false;		// true to load all teams on page load
var googleMapScripts_loadMatches=false;		// true to load all matches on page load
var googleMapScripts_loadUserPosts=false;	// true to load posts of the user in session
var googleMapScripts_loadSinglePostWithID=-1;		// ID >=0 to load the post
var googleMapScripts_loadSinglePostType=0;			// 0 to bring task, 1 to bring team

var googleMapScripts_kmlTasks;				// holds the KML for the tasks
var googleMapScripts_kmlTeams;				// holds the KML for the posts
var googleMapScripts_kmlMatches;			// holds the KML for the matches
var googleMapScripts_kmlUserTasks;			// holds the KML for the user's tasks
var googleMapScripts_kmlUserTeams;			// holds the KML for the user's teams
var googleMapScripts_kmlSinglePost;			// holds the KML for a single post

var googleMapScripts_map;					// the map object
var googleMapScripts_markers = new Array();	// holds all markers in the map except for the user marker
var googleMapScripts_userMarker;			// holds the marker that the user puts on the map
var googleMapScripts_isUserMarkerValid;		// true only if userMarker points to a real position on the map

var username; // the username to load the posts in my posts option

var googleMapScripts_VM_PATH =  "http://109.65.66.103/cs236369_webApp/"; //"http://ibm411.cs.technion.ac.il/ssdl-www-14/cs236369_webApp/";

function initializeMap() {
	var mapOptions = {
	  center: new google.maps.LatLng(32.176630, 34.898140),
	  zoom: 2,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	googleMapScripts_map = new google.maps.Map(document.getElementById("map-canvas"),mapOptions);
	
	// load posts and matches from the DB and add KML layers to the map
	loadAllKML();
	
	// add user marker
	addMarker(new google.maps.LatLng(0,0),true);
	googleMapScripts_userMarker.setVisible(false);
	googleMapScripts_isUserMarkerValid=false;
	
	google.maps.event.addListener(googleMapScripts_map, 'click', function(event){
		moveUserMarker(event.latLng);
	});
}
//google.maps.event.addDomListener(window, 'load', initialize);

function moveUserMarker(pos){
	googleMapScripts_userMarker.setPosition(pos);
	googleMapScripts_userMarker.setVisible(true);
	googleMapScripts_isUserMarkerValid=true;
}

function addMarker(pos,isUserMarker) { 
	var marker = new google.maps.Marker({
		position: pos,
		map: googleMapScripts_map,
	});
	
	if(isUserMarker) {
		googleMapScripts_userMarker=marker;
		googleMapScripts_userMarker.setDraggable(true);
		
		// click to remove marker
		google.maps.event.addListener(marker, 'click', function() {
			marker.setVisible(false);
			googleMapScripts_isUserMarkerValid=false;
		});
	}
	else {
		googleMapScripts_markers.push(marker);
	}
}

function loadAllKML(){
	if(googleMapScripts_loadTasks===true){
		getKMLFromServer(0);
	}
	if(googleMapScripts_loadTeams===true){
		getKMLFromServer(1);
	}
	if(googleMapScripts_loadMatches===true){
		getKMLFromServer(2);
	}
	if(googleMapScripts_loadUserPosts===true){
		getKMLFromServer(3);
		getKMLFromServer(4);
	}
	if(googleMapScripts_loadSinglePostWithID >= 0){
		getKMLFromServer(8);
	}
}

function removeAllKML(){
	if(googleMapScripts_kmlTasks != null){
		googleMapScripts_kmlTasks.setMap(null);
		googleMapScripts_kmlTasks=null;
	}
	if(googleMapScripts_kmlTeams != null){
		googleMapScripts_kmlTeams.setMap(null);
		googleMapScripts_kmlTeams=null;
	}
	if(googleMapScripts_kmlMatches != null){
		googleMapScripts_kmlMatches.setMap(null);
		googleMapScripts_kmlMatches=null;
	}
	if(googleMapScripts_kmlUserTasks != null){
		googleMapScripts_kmlUserTasks.setMap(null);
		googleMapScripts_kmlUserTasks=null;
	}
	if(googleMapScripts_kmlUserTeams != null){
		googleMapScripts_kmlUserTeams.setMap(null);
		googleMapScripts_kmlUserTeams=null;
	}
	if(googleMapScripts_kmlSinglePost != null){
		googleMapScripts_kmlSinglePost.setMap(null);
		googleMapScripts_kmlSinglePost=null;
	}
}

/**
 * @param bringWhat- 0 to bring tasks, 1 to bring teams, 2 to bring matches,
 * 		3 to bring the posts of the user in session, 4 to bring the posts of the user in session,
 * 		5,6,7 to bring posts by search parameters, 8 to bring a single post
 */
function getKMLFromServer(bringWhat, username) {
	switch(bringWhat){
	case 0:
		if(googleMapScripts_kmlTasks != null){
			googleMapScripts_kmlTasks.setMap(null);
			googleMapScripts_kmlTasks=null;
		}
		googleMapScripts_kmlTasks = new google.maps.KmlLayer(googleMapScripts_VM_PATH + "GetPosts?bringWhat=0",
				{
					suppressInfoWindows: false,
					map: googleMapScripts_map
				}
		);
		break;
	case 1:
		if(googleMapScripts_kmlTeams != null){
			googleMapScripts_kmlTeams.setMap(null);
			googleMapScripts_kmlTeams=null;
		}
		googleMapScripts_kmlTeams = new google.maps.KmlLayer(googleMapScripts_VM_PATH + "GetPosts?bringWhat=1",
				{
					suppressInfoWindows: false,
					map: googleMapScripts_map
				}
		);
		break;
	case 2:
		if(googleMapScripts_kmlMatches != null){
			googleMapScripts_kmlMatches.setMap(null);
			googleMapScripts_kmlMatches=null;
		}
		googleMapScripts_kmlMatches = new google.maps.KmlLayer(googleMapScripts_VM_PATH + "GetPosts?bringWhat=2",
				{
					suppressInfoWindows: false,
					map: googleMapScripts_map
				}
		);
		break;
	case 3:		
		if(googleMapScripts_kmlUserTasks != null){
			googleMapScripts_kmlUserTasks.setMap(null);
			googleMapScripts_kmlUserTasks=null;
		}
		googleMapScripts_kmlUserTasks = new google.maps.KmlLayer(googleMapScripts_VM_PATH + "GetPosts?bringWhat=3&username=" + username,
				{
					suppressInfoWindows: false,
					map: googleMapScripts_map
				}
		);
		break;
	case 4:		
		if(googleMapScripts_kmlUserTeams != null){
			googleMapScripts_kmlUserTeams.setMap(null);
			googleMapScripts_kmlUserTeams=null;
		}		
		googleMapScripts_kmlUserTeams = new google.maps.KmlLayer(googleMapScripts_VM_PATH + "GetPosts?bringWhat=4&username=" + username,
				{
					suppressInfoWindows: false,
					map: googleMapScripts_map
				}
		);
		break;
	case 5:
	case 6:
	case 7:
		//reserved for search (look at searchPosts function)
		break;
	case 8:
		if(googleMapScripts_kmlSinglePost != null){
			googleMapScripts_kmlSinglePost.setMap(null);
			googleMapScripts_kmlSinglePost=null;
		}
		googleMapScripts_kmlSinglePost = new google.maps.KmlLayer(googleMapScripts_VM_PATH + 
				"GetPosts?bringWhat=8&postID=" + googleMapScripts_loadSinglePostWithID + 
				"&postType=" + googleMapScripts_loadSinglePostType,
				{
					suppressInfoWindows: false,
					map: googleMapScripts_map
				}
		);		
		break;
	}
}

/**
 * Performs a search and puts KML layers on the map
 * @param radius - radius of the search in meters
 * @param latPos - latitude of center of search
 * @param lngPos - longitude of center of search
 * @param bringTasks - true to bring tasks
 * @param bringTeams - true to bring teams
 * @param bringMatches - true to bring matches
 */
function searchPosts(radius,latPos,lngPos,bringTasks,bringTeams,bringMatches){
	removeAllKML();	
	
	// bring KML objects
	googleMapScripts_kmlTasks = new google.maps.KmlLayer(googleMapScripts_VM_PATH + 
			"GetPosts?bringWhat=5&radius=" + radius + "&latPos=" + latPos + "&lngPos=" + lngPos,
			{
				suppressInfoWindows: false,
			}
	);

	googleMapScripts_kmlTeams = new google.maps.KmlLayer(googleMapScripts_VM_PATH + 
			"GetPosts?bringWhat=6&radius=" + radius + "&latPos=" + latPos + "&lngPos=" + lngPos,
			{
				suppressInfoWindows: false,
			}
	);

	googleMapScripts_kmlMatches = new google.maps.KmlLayer(googleMapScripts_VM_PATH + 
			"GetPosts?bringWhat=7&radius=" + radius + "&latPos=" + latPos + "&lngPos=" + lngPos,
			{
				suppressInfoWindows: false,
			}
	);
	
	// show KML on the map
	setKMLVisible(bringTasks,bringTeams,bringMatches);
}

/**
 * Show or hide KML layer
 * @param showTasks - true to show tasks, false to hide
 * @param showTeams - true to show teams, false to hide
 * @param showMatches - true to show matches, false to hide
 */
function setKMLVisible(showTasks,showTeams,showMatches){
	if(googleMapScripts_kmlTasks != null){
		if(showTasks===true){
			googleMapScripts_kmlTasks.setMap(googleMapScripts_map);
		}
		else{
			googleMapScripts_kmlTasks.setMap(null);
		}
	}
	if(googleMapScripts_kmlTeams != null){
		if(showTeams===true){
			googleMapScripts_kmlTeams.setMap(googleMapScripts_map);
		}
		else{
			googleMapScripts_kmlTeams.setMap(null);
		}
	}
	if(googleMapScripts_kmlMatches != null){
		if(showMatches===true){
			googleMapScripts_kmlMatches.setMap(googleMapScripts_map);
		}
		else{
			googleMapScripts_kmlMatches.setMap(null);
		}
	}
}

function updateUsernameForMaps(user) {
	username = user;
}



